<?php

class controller_user extends controller_theme
{
	public function __construct()
	{
		parent::__construct();
	}
	

	// 登陆页面
	public function login()
	{
		$my = bone::get_user();
		if(!$my->guest)
		{
			$this->redirect( url('./') );
		}

		$template = bone::get_template('user.login');
		$template->set_title('用户登陆');

		$template->set_data($this->get_data());
		$template->display();
	}

	// 登陆检查
	public function login_check()
	{
		$username = post('username', null);
		$password = post('password', null);
		$rememberme = post('rememberme', '0');

		if(!$username || !$password)
			$this->redirect( url('./?controller=user&task=login'), '用户名或密码不能为空', 'error' );
	
		$model = bone::get_model('user');
		if( $model->login($username, $password, $rememberme) )
			$this->redirect( url('./') );
		else
			$this->redirect( url('./?controller=user&task=login'), $model->get_error(), 'error' );
	}


	// 登陆检查
	public function ajax_login_check()
	{
		$username = post('username');
		$password = post('password');
		$rememberme = post('rememberme', '0');

		if(!$username || !$password)
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '用户名或密码不能为空');
			$this->ajax();
		}

		$model = bone::get_model('user');
		if( $model->login($username, $password, $rememberme) )
		{
			$this->set_ajax('status', 0);
			$this->set_ajax('description', '登陆成功');
			$this->ajax();
		}
		else
		{
			$this->set_ajax('status', 3);
			$this->set_ajax('description', $model->get_error() );
			$this->ajax();
		}
	}

	
	// 注册新用户
	public function register()
	{
		$template = bone::get_template('user.register');
		$template->set_title('注册新用户');

		$template->set_data($this->get_data());
		$template->display();
	}

	
	// 保存新注册用户
	public function ajax_register_save()
	{
		$username = post('username', '');
		$email = post('email', '');
		$name = post('name', '');
		$password = post('password', '');
		$confirm_password = post('confirm_password', '');
		
		if($name=='') $name = '匿名';

		if($username=='')
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '用户名不能为空');
			$this->ajax();
		}

		if($email=='')
		{
			$this->set_ajax('status', 2);
			$this->set_ajax('description', '邮箱不能为空');
			$this->ajax();
		}

		if( !preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/i", $email) )
		{
			$this->set_ajax('status', 3);
			$this->set_ajax('description', '邮箱格式非法');
			$this->ajax();
		}

		if($password=='')
		{
			$this->set_ajax('status', 4);
			$this->set_ajax('description', '密码不能为空');
			$this->ajax();
		}

		if($password!=$confirm_password)
		{
			$this->set_ajax('status', 5);
			$this->set_ajax('description', '两次输入的密码不一致');
			$this->ajax();
		}

		$model = bone::get_model('user');
		if( $model->register($username,$email,$password, $name ) )
		{
			$description = '您成功注册了以下账号: ';
			$description .= '<ul>';
			$description .= '<li> 用户名: '.$username.'</li>';
			$description .= '<li> 邮箱: '.$email.'</li>';
			$description .= '<li> 名字: '.$name.'</li>';
			$description .= '</ul>';
			$description .= '<p>点击这里 <a href="'.url('./?controller=user&task=login').'">登陆</a></p>';

			$this->set_ajax('status', 0);
			$this->set_ajax('description', $description);
			$this->ajax();
		}
		else
		{
			$this->set_ajax('status', 6);
			$this->set_ajax('description', $model->get_error() );
			$this->ajax();
		}
	}


	//找回密码表单
	public function forget_password()
	{
		$template = bone::get_template('user.forget_password');
		$template->set_title('找回密码');

		$template->set_data($this->get_data());
		$template->display();
	}
	
	//提交找回密码
	public function ajax_forget_password_save()
	{
		$type = get('type',null);
		$value = get('value','');

		if(($type!='username' && $type!= 'email') || $value=='')
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '参数缺失, 请和管理员联系' );
			$this->ajax();
		}

		$model = bone::get_model('user');
		if( $model->forget_password($type, $value ) )
		{
			$description = '系统已发送了一封重设密码的邮件到您注册时使用的邮箱中， 请查收。';

			$this->set_ajax('status', 0);
			$this->set_ajax('description', $description);
			$this->ajax();
		}
		else
		{
			$this->set_ajax('status', 2);
			$this->set_ajax('description', $model->get_error() );
			$this->ajax();
		}
	}

	// 重设密码
	public function forget_password_reset()
	{
		$user_id = get('user_id',0,'int');
		$key = get('key','');
		if($user_id ==0 || $key=='') exit;

		$user = bone::get_table('user');
		$user->load($user_id);

		if($user->key != $key) exit('该链接已失效');

		$template = bone::get_template('user.forget_password_reset');
		$template->set_title('重设密码');
		$template->set('user', $user);
		$template->set_data($this->get_data());
		$template->display();
	}


	// 重设密码保存
	public function ajax_forget_password_reset_save()
	{
		$user_id = post('user_id',0,'int');
		$key = post('key','');

		if($user_id ==0 || $key=='')
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '参数缺失, 请和管理员联系' );
			$this->ajax();
		}

		$password = post('password', '');
		$confirm_password = post('confirm_password', '');

		if($password!=$confirm_password)
		{
			$this->set_ajax('status', 2);
			$this->set_ajax('description', '两次输入的密码不一致');
			$this->ajax();
		}
		

		$model = bone::get_model('user');
		if( $model->forget_password_reset($user_id, $key, $password ) )
		{
			$description = '重设密码成功， 现在您可以使用新密码 <a href="'.url('./?controller=user&task=login').'">登陆</a> 了。';
			$this->set_ajax('status', 0);
			$this->set_ajax('description', $description);
			$this->ajax();
		}
		else
		{
			$this->set_ajax('status', 3);
			$this->set_ajax('description', $model->get_error() );
			$this->ajax();
		}

	}


	// 修改用户资料
	public function edit()
	{
		$my = bone::get_user();

		$user = bone::get_table('user');
		$user->load($my->id);

		$template = bone::get_template('user.edit');
		$template->set_title('修改用户资料');
		$template->set('user', $user);
		$template->set_data($this->get_data());
		$template->display();
	}


	// 修改用户资料
	public function edit_save()
	{
		$my = bone::get_user();

		$user = bone::get_table('user');
		$user->load($my->id);
		$user->bind(post());
		if(trim($user->name)=='')$user->name = '匿名';

		$avatar = $_FILES['avatar'];
		if($avatar['error']==0)
		{
			$image = bone::get('image');
			$image->open( $avatar['tmp_name'] );
			if( $image->is_image() )
			{
				$image->resize_to(96, 96);
				$image->save_to( BONE_ROOT.DS.'storage'.DS.'avatar'.DS.'large_'.$my->id.'.'.$image->get_type());

				$image->resize_to(64, 64);
				$image->save_to( BONE_ROOT.DS.'storage'.DS.'avatar'.DS.$my->id.'.'.$image->get_type());
				$user->avatar = $my->id.'.'.$image->get_type();
			}
		}
		$user->save();
		$this->redirect( url('./?controller=user&task=edit'), '您的资料已保存');
	}


	// 修改用户资料
	public function reset_password()
	{
		$user = bone::get_user();
		if($user->guest) exit('非法用户');

		$template = bone::get_template('user.reset_password');
		$template->set_title('重设密码');
		$template->set('user', $user);
		$template->set_data($this->get_data());
		$template->display();
	}

	// 修改用户资料
	public function ajax_reset_password_save()
	{
		$password = post('password');
		$confirm_password = post('confirm_password');

		if($password!=$confirm_password)
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '两次输入的密码不一致');
			$this->ajax();
		}
		

		$model = bone::get_model('user');
		if( $model->reset_password( $password ) )
		{
			$description = '重设密码成功， 下次您可以使用新密码登陆了。';
			$this->set_ajax('status', 0);
			$this->set_ajax('description', $description);
			$this->ajax();
		}
		else
		{
			$this->set_ajax('status', 2);
			$this->set_ajax('description', $model->get_error() );
			$this->ajax();
		}

	}


	// 退出登陆
	public function logout()
	{
		$model = bone::get_model('user');
		$model->logout();

		$this->redirect( url('./' ), '成功退出');
	}

	// 更新用户在线状态
	public function ajax_heartbeat()
	{
		$my = bone::get_user();
		if($my->guest)
		{
			$this->set_ajax('status', 1);
			$this->set_ajax('description', '没有登陆' );
			$this->ajax();
		}
		else
		{
			$model = bone::get_model('user');
			$model->heartbeat($my->id);

			$this->set_ajax('status', 0);
			$this->set_ajax('description', '更新成功');
			$this->ajax();
		}
	}


}
?>